//数字数组0-9
function numList() {
    var numList = []
    for (var i = 48; i <= 57; i++) {
        var char = String.fromCharCode(i)
        numList.push(char)
    }
    return numList
}
//小写字母数组
function smallList() {
    var smallList = [];
    for (var i = 97; i <= 122; i++) {
        var char = String.fromCharCode(i)
        smallList.push(char)
    }
    return smallList
}
//大写字母数组
function bigList() {
    var bigList = [];
    for (var i = 65; i <= 90; i++) {
        var char = String.fromCharCode(i)
        bigList.push(char)
    }
    return bigList
}
//普通常用字符数组
function normalList() {
    var normalList = ["_", "$"]
    return normalList.concat(numList(), smallList(), bigList())
}
//随机验证码
function randomCode(n) {
    n = n || 4
    var list = numList().concat(smallList(), bigList())
    var str = ""
    for (var i = 0; i < n; i++) {
        var char = list[Math.round((Math.random() * list.length) - 1)]
        str += char
    }
    return str
}
//随机颜色
function randColor() {
    var str = "#"
    for (var i = 0; i < 6; i++) {
        str += (parseInt(Math.random() * 16)).toString(16)
    }
    return str
}
//监听页面——懒加载
function monitor(Height) {
    var scrollHeight = document.documentElement.scrollHeight;//页面总高
    var clientHeight = document.documentElement.clientHeight;//视口高
    var scrollTop = document.documentElement.scrollTop;//滚动距离
    return scrollHeight - clientHeight - scrollTop <= Height
}
//补零
function addZero(num) {
    return num < 10 ? "0" + num : num
}
// 1.获取当月有多少天 (getDays)  下个月的0号就是上个月的最后一天
function getDays(month, year) {
    year = year || 2022
    var date = new Date(year, month, 0)
    return date.getDate()
}

function setCookie(key, val, expires, path = "/") {
    if (expires) {
        var date = new Date()
        date.setSeconds(date.getSeconds() + expires);
        document.cookie = key + "=" + val + "; expires=" + date.toUTCString() + "; path=" + path
    } else {
        document.cookie = key + "=" + val + "; path=" + path
    }
}

function getCookie(key) {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; ")
        for (var i = 0; i < list.length; i++) {
            var item = list[i]
            var name = item.split("=")[0]
            var val = item.split("=")[1]
            if (name === key) {
                return val;
            }
        }
    }
    return "";
}

function delCookie(key) {
    setCookie(key, "", -1);
}
//------------------------------------------------------------------------------------------------------------

function urlParamsParse() {
    var search = location.search
    var data = {}
    if (search) {
        var str = search.substring(1)
        var list = str.split("&")
        list.forEach(item => {
            var key = item.split("=")[0]
            var val = item.split("=")[1]
            data[key] = decodeURIComponent(val)
        })
    }
    return data
}
//ajax
function ajax(ajaxObject) {
    var { type = 'get', url, data = '', async = true, dataType = 'json', callback, error } = ajaxObject
    if (typeof data == 'object') { // 判断data是对象(不准确)
        var list = []
        for (var key in data) {
            var val = data[key]
            list.push(key + '=' + val)
        }
        data = list.join('&')
    }
    var xhr = new XMLHttpRequest()
    if (type.toLowerCase() == 'get') {
        xhr.open('get', data ? url + '?' + data : url, async)
        xhr.send()
    } else if (type.toLowerCase() == 'post') {
        xhr.open('post', url, async)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(data)
    }
    xhr.onreadystatechange = function () {
        try {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText
                if (dataType == 'json') {
                    result = JSON.parse(result)
                }
                if (callback && typeof callback === 'function') {
                    callback(result)
                }
            }
        } catch (err) {
            if (error && typeof error == 'function') {
                error(err)
            }
        }
    }
}

//Promise
function request(ajaxObject) {
    return new Promise(function (resolve, reject) {
        var { type = "get", url, data = "", async = true, dataType = "json" } = ajaxObject
        if (typeof data == "object") { // 判断data是对象(不准确)
            var list = []
            for (var key in data) {
                var val = data[key]
                list.push(key + "=" + val)
            }
            data = list.join("&")
        }
        var xhr = new XMLHttpRequest()
        if (type.toLowerCase() == "get") {
            xhr.open("get", data ? (url + "?" + data) : url, async)
            xhr.send()
        } else if (type.toLowerCase() == "post") {
            xhr.open("post", url, async)
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            xhr.send(data)
        }
        xhr.onreadystatechange = function () {
            try {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText
                    if (dataType == "json") {
                        result = JSON.parse(result)
                    }
                    resolve(result)
                }
            } catch (err) {
                reject(err)
            }
        }
    })
}

//------------------------------------------------------------